package com.neusoft.admin.controller;

import com.neusoft.grid.entity.*;
import com.neusoft.admin.entity.TableData;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;


public class AQIdistributetotalstatisViewControl {
    @FXML
    private TableView<TableData> tableView;
    private static final String AQI_FILE = "src/com/neusoft/grid/data/AQI.txt";
    private List<AQI> aqilist = new ArrayList<>();

    private int count1 = 0;
    private int count2 = 0;
    private int count3 = 0;
    private int count4 = 0;
    private int count5 = 0;
    private int count6 = 0;

    // 表格的固定高度（根据实际需求调整）
    private static final double TABLE_HEIGHT = 533;
    // 表格头部高度估计值
    private static final double TABLE_HEADER_HEIGHT = 30;
    // 表格底部空白估计值
    private static final double TABLE_PADDING = 10;

    @FXML
    private void initialize() {
        try {
            aqilist = loadAQIData();
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (AQI aqi : aqilist) {
            if (aqi.getAQI() > 0 && aqi.getAQI() <= 50){
                count1++;
            }else if (aqi.getAQI() > 50 && aqi.getAQI() <= 100){
                count2++;
            }else if(aqi.getAQI() > 100 && aqi.getAQI() <= 150){
                count3++;
            }else if(aqi.getAQI() > 150 && aqi.getAQI() <= 200){
                count4++;
            }else if(aqi.getAQI() > 200 && aqi.getAQI() <= 300){
                count5++;
            }else{
                count6++;
            }
        }

        ObservableList<TableData> data = FXCollections.observableArrayList(
                new TableData("1", "优", String.valueOf(count1)),
                new TableData("2", "良", String.valueOf(count2)),
                new TableData("3", "轻度污染", String.valueOf(count3)),
                new TableData("4", "中度污染", String.valueOf(count4)),
                new TableData("5", "重度污染",String.valueOf(count5)),
                new TableData("6", "严重污染", String.valueOf(count6)));
        tableView.setItems(data);
        setupCellFormatting();

        // 设置表格固定高度
        tableView.setPrefHeight(TABLE_HEIGHT);
        tableView.setMaxHeight(TABLE_HEIGHT);
        tableView.setMinHeight(TABLE_HEIGHT);

        // 计算并设置行高
        double rowHeight = (TABLE_HEIGHT - TABLE_HEADER_HEIGHT - TABLE_PADDING) / data.size();
        tableView.setFixedCellSize(rowHeight);

        // 隐藏垂直滚动条
        setupCellFormatting();
        hideVerticalScrollBar();
    }

    private List<AQI> loadAQIData() throws IOException {
        File file = new File(AQI_FILE);
        if (!file.exists() || file.length() == 0) {
            return new ArrayList<>();
        }

        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
            return (List<AQI>) ois.readObject();
        } catch (ClassNotFoundException e) {
            throw new IOException("数据文件格式错误", e);
        }
    }

    private void hideVerticalScrollBar() {
        // 使用Platform.runLater确保在UI渲染完成后执行
        Platform.runLater(() -> {
            for (Node node : tableView.lookupAll(".scroll-bar")) {
                if (node instanceof ScrollBar) {
                    ScrollBar scrollBar = (ScrollBar) node;
                    if (scrollBar.getOrientation().equals(javafx.geometry.Orientation.VERTICAL)) {
                        scrollBar.setMaxWidth(0);
                        scrollBar.setVisible(false);
                        scrollBar.setManaged(false);
                    }
                }
            }
        });
    }

    private void setupCellFormatting() {
        for (TableColumn column : tableView.getColumns()) { // 移除泛型参数
            column.setCellFactory(tc -> {
                TableCell cell = new TableCell<TableData, Object>() {
                    @Override
                    protected void updateItem(Object item, boolean empty) {
                        super.updateItem(item, empty);
                        if (empty) {
                            setText(null);
                        } else {
                            setText(item.toString());
                            setAlignment(javafx.geometry.Pos.CENTER);
                            setStyle("-fx-font-size: 18px;");
                        }
                    }
                };
                return cell;
            });
        }
    }
}